﻿using System;
using System.Text;

namespace GPS.GPSLanguage
{
    class PRNToken
    {
        internal static void ParseLanguage(GPSData gpsData, System.Collections.Queue q)
        {
            switch (gpsData.MessageType)
            {
                case MessageType.GPGSA:
                    HandleGSA(gpsData, q);
                    break;
                case MessageType.GPGSV:
                    HandleGSV(gpsData, q);
                    break;
                default:
                    throw new InvalidLanguageException("How did you get here?");
            }

        }

        private static void HandleGSV(GPSData gpsData, System.Collections.Queue q)
        {
            int curSatellite = gpsData.CurrentSatelliteCount; 
            int endLoop = Math.Min(curSatellite + 4, gpsData.SatelliteCount);
            for (int i = curSatellite; i < endLoop; i++)
            {
                String prnToken = (String)q.Dequeue();
                int prnNumber = -1;
                try
                {
                    if(prnToken.Length>0)
                        prnNumber = int.Parse(prnToken);
                }
                catch { }

                gpsData.SetSatellitePRN(i, prnNumber);
                ElevationToken.ParseLanguage(gpsData, q);
                AzimuthToken.ParseLanguage(gpsData, q);
                SNRToken.ParseLanguage(gpsData, q);
                gpsData.CurrentSatelliteCount++;
            }
            

        }

        private static void HandleGSA(GPSData gpsData, System.Collections.Queue q)
        {
            for (int i = 0; i < 12; i++)
            {
                String prnToken = (String)q.Dequeue();
                int prnNumber = -1;
                try
                {
                    if (prnToken.Length > 0)
                        prnNumber = int.Parse(prnToken);
                }
                catch { }

                gpsData.SetSatellitePRN(i, prnNumber);
            }
            PDOPToken.ParseLanguage(gpsData, q);

        }

    }
}
